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.

311 lines
8.6KB

  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. init_get_bits8(gb, avpkt->data + 8, avpkt->size - 8);
  165. skip_bits(gb, 24);
  166. first_symbol = get_bits(gb, 8);
  167. s->nb_symbols = get_bits(gb, 8) + 1;
  168. symbol = first_symbol;
  169. for (int i = 0; i < s->nb_symbols; symbol++) {
  170. int prob;
  171. if (get_bits1(gb)) {
  172. prob = get_bits(gb, 12);
  173. } else {
  174. prob = get_bits(gb, 3);
  175. }
  176. if (prob) {
  177. s->symb[i] = symbol;
  178. s->prob[i] = prob;
  179. i++;
  180. }
  181. }
  182. ret = build_vlc(avctx, &s->vlc);
  183. if (ret < 0)
  184. return ret;
  185. for (int p = 0; p < 3; p++) {
  186. int width = avctx->width >> (p > 0);
  187. ptrdiff_t stride = frame->linesize[p];
  188. uint8_t *dst;
  189. dst = frame->data[p] + (avctx->height - 1) * frame->linesize[p];
  190. for (int y = 0; y < avctx->height; y++) {
  191. for (int x = 0; x < width; x++) {
  192. int v = get_vlc2(gb, s->vlc.table, s->vlc.bits, 3);
  193. if (v < 0)
  194. return AVERROR_INVALIDDATA;
  195. dst[x] = v;
  196. }
  197. dst -= stride;
  198. }
  199. }
  200. } else {
  201. return AVERROR_INVALIDDATA;
  202. }
  203. for (int p = 0; p < 3; p++) {
  204. int left, lefttop;
  205. int width = avctx->width >> (p > 0);
  206. ptrdiff_t stride = frame->linesize[p];
  207. uint8_t *dst;
  208. dst = frame->data[p] + (avctx->height - 1) * frame->linesize[p];
  209. s->llviddsp.add_left_pred(dst, dst, width, 0);
  210. dst -= stride;
  211. lefttop = left = dst[0];
  212. for (int y = 1; y < avctx->height; y++) {
  213. s->llviddsp.add_median_pred(dst, dst + stride, dst, width, &left, &lefttop);
  214. lefttop = left = dst[0];
  215. dst -= stride;
  216. }
  217. }
  218. frame->pict_type = AV_PICTURE_TYPE_I;
  219. frame->key_frame = 1;
  220. *got_frame = 1;
  221. return avpkt->size;
  222. }
  223. static av_cold int decode_init(AVCodecContext *avctx)
  224. {
  225. MVHAContext *s = avctx->priv_data;
  226. int zret;
  227. avctx->pix_fmt = AV_PIX_FMT_YUV422P;
  228. s->zstream.zalloc = Z_NULL;
  229. s->zstream.zfree = Z_NULL;
  230. s->zstream.opaque = Z_NULL;
  231. zret = inflateInit(&s->zstream);
  232. if (zret != Z_OK) {
  233. av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
  234. return AVERROR_EXTERNAL;
  235. }
  236. ff_llviddsp_init(&s->llviddsp);
  237. return 0;
  238. }
  239. static av_cold int decode_close(AVCodecContext *avctx)
  240. {
  241. MVHAContext *s = avctx->priv_data;
  242. inflateEnd(&s->zstream);
  243. ff_free_vlc(&s->vlc);
  244. return 0;
  245. }
  246. AVCodec ff_mvha_decoder = {
  247. .name = "mvha",
  248. .long_name = NULL_IF_CONFIG_SMALL("MidiVid Archive Codec"),
  249. .type = AVMEDIA_TYPE_VIDEO,
  250. .id = AV_CODEC_ID_MVHA,
  251. .priv_data_size = sizeof(MVHAContext),
  252. .init = decode_init,
  253. .close = decode_close,
  254. .decode = decode_frame,
  255. .capabilities = AV_CODEC_CAP_DR1,
  256. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
  257. FF_CODEC_CAP_INIT_CLEANUP,
  258. };