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.

337 lines
11KB

  1. /*
  2. * Feeble Files/ScummVM DXA decoder
  3. * Copyright (c) 2007 Konstantin Shishkov
  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. /**
  22. * @file
  23. * DXA Video decoder
  24. */
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include "libavutil/common.h"
  28. #include "libavutil/intreadwrite.h"
  29. #include "bytestream.h"
  30. #include "avcodec.h"
  31. #include "internal.h"
  32. #include <zlib.h>
  33. /*
  34. * Decoder context
  35. */
  36. typedef struct DxaDecContext {
  37. AVFrame prev;
  38. int dsize;
  39. uint8_t *decomp_buf;
  40. uint32_t pal[256];
  41. } DxaDecContext;
  42. static const int shift1[6] = { 0, 8, 8, 8, 4, 4 };
  43. static const int shift2[6] = { 0, 0, 8, 4, 0, 4 };
  44. static int decode_13(AVCodecContext *avctx, DxaDecContext *c, uint8_t* dst,
  45. int stride, uint8_t *src, uint8_t *ref)
  46. {
  47. uint8_t *code, *data, *mv, *msk, *tmp, *tmp2;
  48. int i, j, k;
  49. int type, x, y, d, d2;
  50. uint32_t mask;
  51. code = src + 12;
  52. data = code + ((avctx->width * avctx->height) >> 4);
  53. mv = data + AV_RB32(src + 0);
  54. msk = mv + AV_RB32(src + 4);
  55. for(j = 0; j < avctx->height; j += 4){
  56. for(i = 0; i < avctx->width; i += 4){
  57. tmp = dst + i;
  58. tmp2 = ref + i;
  59. type = *code++;
  60. switch(type){
  61. case 4: // motion compensation
  62. x = (*mv) >> 4; if(x & 8) x = 8 - x;
  63. y = (*mv++) & 0xF; if(y & 8) y = 8 - y;
  64. if (i < -x || avctx->width - i - 4 < x ||
  65. j < -y || avctx->height - j - 4 < y) {
  66. av_log(avctx, AV_LOG_ERROR, "MV %d %d out of bounds\n", x,y);
  67. return AVERROR_INVALIDDATA;
  68. }
  69. tmp2 += x + y*stride;
  70. case 0: // skip
  71. case 5: // skip in method 12
  72. for(y = 0; y < 4; y++){
  73. memcpy(tmp, tmp2, 4);
  74. tmp += stride;
  75. tmp2 += stride;
  76. }
  77. break;
  78. case 1: // masked change
  79. case 10: // masked change with only half of pixels changed
  80. case 11: // cases 10-15 are for method 12 only
  81. case 12:
  82. case 13:
  83. case 14:
  84. case 15:
  85. if(type == 1){
  86. mask = AV_RB16(msk);
  87. msk += 2;
  88. }else{
  89. type -= 10;
  90. mask = ((msk[0] & 0xF0) << shift1[type]) | ((msk[0] & 0xF) << shift2[type]);
  91. msk++;
  92. }
  93. for(y = 0; y < 4; y++){
  94. for(x = 0; x < 4; x++){
  95. tmp[x] = (mask & 0x8000) ? *data++ : tmp2[x];
  96. mask <<= 1;
  97. }
  98. tmp += stride;
  99. tmp2 += stride;
  100. }
  101. break;
  102. case 2: // fill block
  103. for(y = 0; y < 4; y++){
  104. memset(tmp, data[0], 4);
  105. tmp += stride;
  106. }
  107. data++;
  108. break;
  109. case 3: // raw block
  110. for(y = 0; y < 4; y++){
  111. memcpy(tmp, data, 4);
  112. data += 4;
  113. tmp += stride;
  114. }
  115. break;
  116. case 8: // subblocks - method 13 only
  117. mask = *msk++;
  118. for(k = 0; k < 4; k++){
  119. d = ((k & 1) << 1) + ((k & 2) * stride);
  120. d2 = ((k & 1) << 1) + ((k & 2) * stride);
  121. tmp2 = ref + i + d2;
  122. switch(mask & 0xC0){
  123. case 0x80: // motion compensation
  124. x = (*mv) >> 4; if(x & 8) x = 8 - x;
  125. y = (*mv++) & 0xF; if(y & 8) y = 8 - y;
  126. tmp2 += x + y*stride;
  127. case 0x00: // skip
  128. tmp[d + 0 ] = tmp2[0];
  129. tmp[d + 1 ] = tmp2[1];
  130. tmp[d + 0 + stride] = tmp2[0 + stride];
  131. tmp[d + 1 + stride] = tmp2[1 + stride];
  132. break;
  133. case 0x40: // fill
  134. tmp[d + 0 ] = data[0];
  135. tmp[d + 1 ] = data[0];
  136. tmp[d + 0 + stride] = data[0];
  137. tmp[d + 1 + stride] = data[0];
  138. data++;
  139. break;
  140. case 0xC0: // raw
  141. tmp[d + 0 ] = *data++;
  142. tmp[d + 1 ] = *data++;
  143. tmp[d + 0 + stride] = *data++;
  144. tmp[d + 1 + stride] = *data++;
  145. break;
  146. }
  147. mask <<= 2;
  148. }
  149. break;
  150. case 32: // vector quantization - 2 colors
  151. mask = AV_RB16(msk);
  152. msk += 2;
  153. for(y = 0; y < 4; y++){
  154. for(x = 0; x < 4; x++){
  155. tmp[x] = data[mask & 1];
  156. mask >>= 1;
  157. }
  158. tmp += stride;
  159. tmp2 += stride;
  160. }
  161. data += 2;
  162. break;
  163. case 33: // vector quantization - 3 or 4 colors
  164. case 34:
  165. mask = AV_RB32(msk);
  166. msk += 4;
  167. for(y = 0; y < 4; y++){
  168. for(x = 0; x < 4; x++){
  169. tmp[x] = data[mask & 3];
  170. mask >>= 2;
  171. }
  172. tmp += stride;
  173. tmp2 += stride;
  174. }
  175. data += type - 30;
  176. break;
  177. default:
  178. av_log(avctx, AV_LOG_ERROR, "Unknown opcode %d\n", type);
  179. return AVERROR_INVALIDDATA;
  180. }
  181. }
  182. dst += stride * 4;
  183. ref += stride * 4;
  184. }
  185. return 0;
  186. }
  187. static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
  188. {
  189. AVFrame *frame = data;
  190. DxaDecContext * const c = avctx->priv_data;
  191. uint8_t *outptr, *srcptr, *tmpptr;
  192. unsigned long dsize;
  193. int i, j, compr, ret;
  194. int stride;
  195. int pc = 0;
  196. GetByteContext gb;
  197. bytestream2_init(&gb, avpkt->data, avpkt->size);
  198. /* make the palette available on the way out */
  199. if (bytestream2_peek_le32(&gb) == MKTAG('C','M','A','P')) {
  200. bytestream2_skip(&gb, 4);
  201. for(i = 0; i < 256; i++){
  202. c->pal[i] = 0xFFU << 24 | bytestream2_get_be24(&gb);
  203. }
  204. pc = 1;
  205. }
  206. if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
  207. return ret;
  208. memcpy(frame->data[1], c->pal, AVPALETTE_SIZE);
  209. frame->palette_has_changed = pc;
  210. outptr = frame->data[0];
  211. srcptr = c->decomp_buf;
  212. tmpptr = c->prev.data[0];
  213. stride = frame->linesize[0];
  214. if (bytestream2_get_le32(&gb) == MKTAG('N','U','L','L'))
  215. compr = -1;
  216. else
  217. compr = bytestream2_get_byte(&gb);
  218. dsize = c->dsize;
  219. if (compr != 4 && compr != -1) {
  220. bytestream2_skip(&gb, 4);
  221. if (uncompress(c->decomp_buf, &dsize, avpkt->data + bytestream2_tell(&gb),
  222. bytestream2_get_bytes_left(&gb)) != Z_OK) {
  223. av_log(avctx, AV_LOG_ERROR, "Uncompress failed!\n");
  224. return AVERROR_UNKNOWN;
  225. }
  226. }
  227. switch(compr){
  228. case -1:
  229. frame->key_frame = 0;
  230. frame->pict_type = AV_PICTURE_TYPE_P;
  231. if(c->prev.data[0])
  232. memcpy(frame->data[0], c->prev.data[0], frame->linesize[0] * avctx->height);
  233. else{ // Should happen only when first frame is 'NULL'
  234. memset(frame->data[0], 0, frame->linesize[0] * avctx->height);
  235. frame->key_frame = 1;
  236. frame->pict_type = AV_PICTURE_TYPE_I;
  237. }
  238. break;
  239. case 2:
  240. case 3:
  241. case 4:
  242. case 5:
  243. frame->key_frame = !(compr & 1);
  244. frame->pict_type = (compr & 1) ? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
  245. for(j = 0; j < avctx->height; j++){
  246. if((compr & 1) && tmpptr){
  247. for(i = 0; i < avctx->width; i++)
  248. outptr[i] = srcptr[i] ^ tmpptr[i];
  249. tmpptr += stride;
  250. }else
  251. memcpy(outptr, srcptr, avctx->width);
  252. outptr += stride;
  253. srcptr += avctx->width;
  254. }
  255. break;
  256. case 12: // ScummVM coding
  257. case 13:
  258. frame->key_frame = 0;
  259. frame->pict_type = AV_PICTURE_TYPE_P;
  260. if (!c->prev.data[0]) {
  261. av_log(avctx, AV_LOG_ERROR, "Missing reference frame\n");
  262. return AVERROR_INVALIDDATA;
  263. }
  264. decode_13(avctx, c, frame->data[0], frame->linesize[0], srcptr, c->prev.data[0]);
  265. break;
  266. default:
  267. av_log(avctx, AV_LOG_ERROR, "Unknown/unsupported compression type %d\n", compr);
  268. return AVERROR_INVALIDDATA;
  269. }
  270. av_frame_unref(&c->prev);
  271. if ((ret = av_frame_ref(&c->prev, frame)) < 0)
  272. return ret;
  273. *got_frame = 1;
  274. /* always report that the buffer was completely consumed */
  275. return avpkt->size;
  276. }
  277. static av_cold int decode_init(AVCodecContext *avctx)
  278. {
  279. DxaDecContext * const c = avctx->priv_data;
  280. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  281. avcodec_get_frame_defaults(&c->prev);
  282. c->dsize = avctx->width * avctx->height * 2;
  283. c->decomp_buf = av_malloc(c->dsize);
  284. if (!c->decomp_buf) {
  285. av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
  286. return AVERROR(ENOMEM);
  287. }
  288. return 0;
  289. }
  290. static av_cold int decode_end(AVCodecContext *avctx)
  291. {
  292. DxaDecContext * const c = avctx->priv_data;
  293. av_freep(&c->decomp_buf);
  294. av_frame_unref(&c->prev);
  295. return 0;
  296. }
  297. AVCodec ff_dxa_decoder = {
  298. .name = "dxa",
  299. .type = AVMEDIA_TYPE_VIDEO,
  300. .id = AV_CODEC_ID_DXA,
  301. .priv_data_size = sizeof(DxaDecContext),
  302. .init = decode_init,
  303. .close = decode_end,
  304. .decode = decode_frame,
  305. .capabilities = CODEC_CAP_DR1,
  306. .long_name = NULL_IF_CONFIG_SMALL("Feeble Files/ScummVM DXA"),
  307. };