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.

341 lines
11KB

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