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.

316 lines
8.7KB

  1. /*
  2. * MidiVid Archive codec
  3. *
  4. * Copyright (c) 2019 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. #define CACHED_BITSTREAM_READER !ARCH_X86_32
  26. #include "libavutil/intreadwrite.h"
  27. #include "avcodec.h"
  28. #include "bytestream.h"
  29. #include "get_bits.h"
  30. #include "internal.h"
  31. #include "lossless_videodsp.h"
  32. #include <zlib.h>
  33. typedef struct MVHAContext {
  34. GetBitContext gb;
  35. int nb_symbols;
  36. uint8_t symb[256];
  37. uint32_t prob[256];
  38. VLC vlc;
  39. z_stream zstream;
  40. LLVidDSPContext llviddsp;
  41. } MVHAContext;
  42. typedef struct Node {
  43. int16_t sym;
  44. int16_t n0;
  45. int16_t l, r;
  46. uint32_t count;
  47. } Node;
  48. static void get_tree_codes(uint32_t *bits, int16_t *lens, uint8_t *xlat,
  49. Node *nodes, int node,
  50. uint32_t pfx, int pl, int *pos)
  51. {
  52. int s;
  53. s = nodes[node].sym;
  54. if (s != -1) {
  55. bits[*pos] = (~pfx) & ((1ULL << FFMAX(pl, 1)) - 1);
  56. lens[*pos] = FFMAX(pl, 1);
  57. xlat[*pos] = s + (pl == 0);
  58. (*pos)++;
  59. } else {
  60. pfx <<= 1;
  61. pl++;
  62. get_tree_codes(bits, lens, xlat, nodes, nodes[node].l, pfx, pl,
  63. pos);
  64. pfx |= 1;
  65. get_tree_codes(bits, lens, xlat, nodes, nodes[node].r, pfx, pl,
  66. pos);
  67. }
  68. }
  69. static int build_vlc(AVCodecContext *avctx, VLC *vlc)
  70. {
  71. MVHAContext *s = avctx->priv_data;
  72. Node nodes[512];
  73. uint32_t bits[256];
  74. int16_t lens[256];
  75. uint8_t xlat[256];
  76. int cur_node, i, j, pos = 0;
  77. ff_free_vlc(vlc);
  78. for (i = 0; i < s->nb_symbols; i++) {
  79. nodes[i].count = s->prob[i];
  80. nodes[i].sym = s->symb[i];
  81. nodes[i].n0 = -2;
  82. nodes[i].l = i;
  83. nodes[i].r = i;
  84. }
  85. cur_node = s->nb_symbols;
  86. j = 0;
  87. do {
  88. for (i = 0; ; i++) {
  89. int new_node = j;
  90. int first_node = cur_node;
  91. int second_node = cur_node;
  92. unsigned nd, st;
  93. nodes[cur_node].count = -1;
  94. do {
  95. int val = nodes[new_node].count;
  96. if (val && (val < nodes[first_node].count)) {
  97. if (val >= nodes[second_node].count) {
  98. first_node = new_node;
  99. } else {
  100. first_node = second_node;
  101. second_node = new_node;
  102. }
  103. }
  104. new_node += 1;
  105. } while (new_node != cur_node);
  106. if (first_node == cur_node)
  107. break;
  108. nd = nodes[second_node].count;
  109. st = nodes[first_node].count;
  110. nodes[second_node].count = 0;
  111. nodes[first_node].count = 0;
  112. if (nd >= UINT32_MAX - st) {
  113. av_log(avctx, AV_LOG_ERROR, "count overflow\n");
  114. return AVERROR_INVALIDDATA;
  115. }
  116. nodes[cur_node].count = nd + st;
  117. nodes[cur_node].sym = -1;
  118. nodes[cur_node].n0 = cur_node;
  119. nodes[cur_node].l = first_node;
  120. nodes[cur_node].r = second_node;
  121. cur_node++;
  122. }
  123. j++;
  124. } while (cur_node - s->nb_symbols == j);
  125. get_tree_codes(bits, lens, xlat, nodes, cur_node - 1, 0, 0, &pos);
  126. return ff_init_vlc_sparse(vlc, 12, pos, lens, 2, 2, bits, 4, 4, xlat, 1, 1, 0);
  127. }
  128. static int decode_frame(AVCodecContext *avctx,
  129. void *data, int *got_frame,
  130. AVPacket *avpkt)
  131. {
  132. MVHAContext *s = avctx->priv_data;
  133. AVFrame *frame = data;
  134. uint32_t type, size;
  135. int ret;
  136. if (avpkt->size <= 8)
  137. return AVERROR_INVALIDDATA;
  138. type = AV_RB32(avpkt->data);
  139. size = AV_RL32(avpkt->data + 4);
  140. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  141. return ret;
  142. if (type == MKTAG('L','Z','Y','V')) {
  143. ret = inflateReset(&s->zstream);
  144. if (ret != Z_OK) {
  145. av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", ret);
  146. return AVERROR_EXTERNAL;
  147. }
  148. s->zstream.next_in = avpkt->data + 8;
  149. s->zstream.avail_in = avpkt->size - 8;
  150. for (int p = 0; p < 3; p++) {
  151. for (int y = 0; y < avctx->height; y++) {
  152. s->zstream.next_out = frame->data[p] + (avctx->height - y - 1) * frame->linesize[p];
  153. s->zstream.avail_out = avctx->width >> (p > 0);
  154. ret = inflate(&s->zstream, Z_SYNC_FLUSH);
  155. if (ret != Z_OK && ret != Z_STREAM_END) {
  156. av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", ret);
  157. return AVERROR_EXTERNAL;
  158. }
  159. }
  160. }
  161. } else if (type == MKTAG('H','U','F','Y')) {
  162. GetBitContext *gb = &s->gb;
  163. int first_symbol, symbol;
  164. ret = init_get_bits8(gb, avpkt->data + 8, avpkt->size - 8);
  165. if (ret < 0)
  166. return ret;
  167. skip_bits(gb, 24);
  168. first_symbol = get_bits(gb, 8);
  169. s->nb_symbols = get_bits(gb, 8) + 1;
  170. symbol = first_symbol;
  171. for (int i = 0; i < s->nb_symbols; symbol++) {
  172. int prob;
  173. if (get_bits_left(gb) < 4)
  174. return AVERROR_INVALIDDATA;
  175. if (get_bits1(gb)) {
  176. prob = get_bits(gb, 12);
  177. } else {
  178. prob = get_bits(gb, 3);
  179. }
  180. if (prob) {
  181. s->symb[i] = symbol;
  182. s->prob[i] = prob;
  183. i++;
  184. }
  185. }
  186. ret = build_vlc(avctx, &s->vlc);
  187. if (ret < 0)
  188. return ret;
  189. for (int p = 0; p < 3; p++) {
  190. int width = avctx->width >> (p > 0);
  191. ptrdiff_t stride = frame->linesize[p];
  192. uint8_t *dst;
  193. dst = frame->data[p] + (avctx->height - 1) * frame->linesize[p];
  194. for (int y = 0; y < avctx->height; y++) {
  195. for (int x = 0; x < width; x++) {
  196. int v = get_vlc2(gb, s->vlc.table, s->vlc.bits, 3);
  197. if (v < 0)
  198. return AVERROR_INVALIDDATA;
  199. dst[x] = v;
  200. }
  201. dst -= stride;
  202. }
  203. }
  204. } else {
  205. return AVERROR_INVALIDDATA;
  206. }
  207. for (int p = 0; p < 3; p++) {
  208. int left, lefttop;
  209. int width = avctx->width >> (p > 0);
  210. ptrdiff_t stride = frame->linesize[p];
  211. uint8_t *dst;
  212. dst = frame->data[p] + (avctx->height - 1) * frame->linesize[p];
  213. s->llviddsp.add_left_pred(dst, dst, width, 0);
  214. dst -= stride;
  215. lefttop = left = dst[0];
  216. for (int y = 1; y < avctx->height; y++) {
  217. s->llviddsp.add_median_pred(dst, dst + stride, dst, width, &left, &lefttop);
  218. lefttop = left = dst[0];
  219. dst -= stride;
  220. }
  221. }
  222. frame->pict_type = AV_PICTURE_TYPE_I;
  223. frame->key_frame = 1;
  224. *got_frame = 1;
  225. return avpkt->size;
  226. }
  227. static av_cold int decode_init(AVCodecContext *avctx)
  228. {
  229. MVHAContext *s = avctx->priv_data;
  230. int zret;
  231. avctx->pix_fmt = AV_PIX_FMT_YUV422P;
  232. s->zstream.zalloc = Z_NULL;
  233. s->zstream.zfree = Z_NULL;
  234. s->zstream.opaque = Z_NULL;
  235. zret = inflateInit(&s->zstream);
  236. if (zret != Z_OK) {
  237. av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
  238. return AVERROR_EXTERNAL;
  239. }
  240. ff_llviddsp_init(&s->llviddsp);
  241. return 0;
  242. }
  243. static av_cold int decode_close(AVCodecContext *avctx)
  244. {
  245. MVHAContext *s = avctx->priv_data;
  246. inflateEnd(&s->zstream);
  247. ff_free_vlc(&s->vlc);
  248. return 0;
  249. }
  250. AVCodec ff_mvha_decoder = {
  251. .name = "mvha",
  252. .long_name = NULL_IF_CONFIG_SMALL("MidiVid Archive Codec"),
  253. .type = AVMEDIA_TYPE_VIDEO,
  254. .id = AV_CODEC_ID_MVHA,
  255. .priv_data_size = sizeof(MVHAContext),
  256. .init = decode_init,
  257. .close = decode_close,
  258. .decode = decode_frame,
  259. .capabilities = AV_CODEC_CAP_DR1,
  260. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
  261. FF_CODEC_CAP_INIT_CLEANUP,
  262. };