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.

362 lines
12KB

  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. if (i + 2*(k & 1) < -x || avctx->width - i - 2*(k & 1) - 2 < x ||
  127. j + (k & 2) < -y || avctx->height - j - (k & 2) - 2 < y) {
  128. av_log(avctx, AV_LOG_ERROR, "MV %d %d out of bounds\n", x,y);
  129. return AVERROR_INVALIDDATA;
  130. }
  131. tmp2 += x + y*stride;
  132. case 0x00: // skip
  133. tmp[d + 0 ] = tmp2[0];
  134. tmp[d + 1 ] = tmp2[1];
  135. tmp[d + 0 + stride] = tmp2[0 + stride];
  136. tmp[d + 1 + stride] = tmp2[1 + stride];
  137. break;
  138. case 0x40: // fill
  139. tmp[d + 0 ] = data[0];
  140. tmp[d + 1 ] = data[0];
  141. tmp[d + 0 + stride] = data[0];
  142. tmp[d + 1 + stride] = data[0];
  143. data++;
  144. break;
  145. case 0xC0: // raw
  146. tmp[d + 0 ] = *data++;
  147. tmp[d + 1 ] = *data++;
  148. tmp[d + 0 + stride] = *data++;
  149. tmp[d + 1 + stride] = *data++;
  150. break;
  151. }
  152. mask <<= 2;
  153. }
  154. break;
  155. case 32: // vector quantization - 2 colors
  156. mask = AV_RB16(msk);
  157. msk += 2;
  158. for(y = 0; y < 4; y++){
  159. for(x = 0; x < 4; x++){
  160. tmp[x] = data[mask & 1];
  161. mask >>= 1;
  162. }
  163. tmp += stride;
  164. tmp2 += stride;
  165. }
  166. data += 2;
  167. break;
  168. case 33: // vector quantization - 3 or 4 colors
  169. case 34:
  170. mask = AV_RB32(msk);
  171. msk += 4;
  172. for(y = 0; y < 4; y++){
  173. for(x = 0; x < 4; x++){
  174. tmp[x] = data[mask & 3];
  175. mask >>= 2;
  176. }
  177. tmp += stride;
  178. tmp2 += stride;
  179. }
  180. data += type - 30;
  181. break;
  182. default:
  183. av_log(avctx, AV_LOG_ERROR, "Unknown opcode %d\n", type);
  184. return AVERROR_INVALIDDATA;
  185. }
  186. }
  187. dst += stride * 4;
  188. ref += stride * 4;
  189. }
  190. return 0;
  191. }
  192. static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
  193. {
  194. AVFrame *frame = data;
  195. DxaDecContext * const c = avctx->priv_data;
  196. uint8_t *outptr, *srcptr, *tmpptr;
  197. unsigned long dsize;
  198. int i, j, compr, ret;
  199. int stride;
  200. int pc = 0;
  201. GetByteContext gb;
  202. bytestream2_init(&gb, avpkt->data, avpkt->size);
  203. /* make the palette available on the way out */
  204. if (bytestream2_peek_le32(&gb) == MKTAG('C','M','A','P')) {
  205. bytestream2_skip(&gb, 4);
  206. for(i = 0; i < 256; i++){
  207. c->pal[i] = 0xFFU << 24 | bytestream2_get_be24(&gb);
  208. }
  209. pc = 1;
  210. }
  211. if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
  212. return ret;
  213. memcpy(frame->data[1], c->pal, AVPALETTE_SIZE);
  214. frame->palette_has_changed = pc;
  215. outptr = frame->data[0];
  216. srcptr = c->decomp_buf;
  217. tmpptr = c->prev->data[0];
  218. stride = frame->linesize[0];
  219. if (bytestream2_get_le32(&gb) == MKTAG('N','U','L','L'))
  220. compr = -1;
  221. else
  222. compr = bytestream2_get_byte(&gb);
  223. dsize = c->dsize;
  224. if (compr != 4 && compr != -1) {
  225. bytestream2_skip(&gb, 4);
  226. if (uncompress(c->decomp_buf, &dsize, avpkt->data + bytestream2_tell(&gb),
  227. bytestream2_get_bytes_left(&gb)) != Z_OK) {
  228. av_log(avctx, AV_LOG_ERROR, "Uncompress failed!\n");
  229. return AVERROR_UNKNOWN;
  230. }
  231. }
  232. if (avctx->debug & FF_DEBUG_PICT_INFO)
  233. av_log(avctx, AV_LOG_DEBUG, "compr:%2d, dsize:%d\n", compr, (int)dsize);
  234. switch(compr){
  235. case -1:
  236. frame->key_frame = 0;
  237. frame->pict_type = AV_PICTURE_TYPE_P;
  238. if (c->prev->data[0])
  239. memcpy(frame->data[0], c->prev->data[0], frame->linesize[0] * avctx->height);
  240. else{ // Should happen only when first frame is 'NULL'
  241. memset(frame->data[0], 0, frame->linesize[0] * avctx->height);
  242. frame->key_frame = 1;
  243. frame->pict_type = AV_PICTURE_TYPE_I;
  244. }
  245. break;
  246. case 2:
  247. case 4:
  248. frame->key_frame = 1;
  249. frame->pict_type = AV_PICTURE_TYPE_I;
  250. for (j = 0; j < avctx->height; j++) {
  251. memcpy(outptr, srcptr, avctx->width);
  252. outptr += stride;
  253. srcptr += avctx->width;
  254. }
  255. break;
  256. case 3:
  257. case 5:
  258. if (!tmpptr) {
  259. av_log(avctx, AV_LOG_ERROR, "Missing reference frame.\n");
  260. if (!(avctx->flags2 & CODEC_FLAG2_SHOW_ALL))
  261. return AVERROR_INVALIDDATA;
  262. }
  263. frame->key_frame = 0;
  264. frame->pict_type = AV_PICTURE_TYPE_P;
  265. for (j = 0; j < avctx->height; j++) {
  266. if(tmpptr){
  267. for(i = 0; i < avctx->width; i++)
  268. outptr[i] = srcptr[i] ^ tmpptr[i];
  269. tmpptr += stride;
  270. }else
  271. memcpy(outptr, srcptr, avctx->width);
  272. outptr += stride;
  273. srcptr += avctx->width;
  274. }
  275. break;
  276. case 12: // ScummVM coding
  277. case 13:
  278. frame->key_frame = 0;
  279. frame->pict_type = AV_PICTURE_TYPE_P;
  280. if (!c->prev->data[0]) {
  281. av_log(avctx, AV_LOG_ERROR, "Missing reference frame\n");
  282. return AVERROR_INVALIDDATA;
  283. }
  284. decode_13(avctx, c, frame->data[0], frame->linesize[0], srcptr, c->prev->data[0]);
  285. break;
  286. default:
  287. av_log(avctx, AV_LOG_ERROR, "Unknown/unsupported compression type %d\n", compr);
  288. return AVERROR_INVALIDDATA;
  289. }
  290. av_frame_unref(c->prev);
  291. if ((ret = av_frame_ref(c->prev, frame)) < 0)
  292. return ret;
  293. *got_frame = 1;
  294. /* always report that the buffer was completely consumed */
  295. return avpkt->size;
  296. }
  297. static av_cold int decode_init(AVCodecContext *avctx)
  298. {
  299. DxaDecContext * const c = avctx->priv_data;
  300. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  301. c->prev = av_frame_alloc();
  302. if (!c->prev)
  303. return AVERROR(ENOMEM);
  304. c->dsize = avctx->width * avctx->height * 2;
  305. c->decomp_buf = av_malloc(c->dsize);
  306. if (!c->decomp_buf) {
  307. av_frame_free(&c->prev);
  308. av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
  309. return AVERROR(ENOMEM);
  310. }
  311. return 0;
  312. }
  313. static av_cold int decode_end(AVCodecContext *avctx)
  314. {
  315. DxaDecContext * const c = avctx->priv_data;
  316. av_freep(&c->decomp_buf);
  317. av_frame_free(&c->prev);
  318. return 0;
  319. }
  320. AVCodec ff_dxa_decoder = {
  321. .name = "dxa",
  322. .long_name = NULL_IF_CONFIG_SMALL("Feeble Files/ScummVM DXA"),
  323. .type = AVMEDIA_TYPE_VIDEO,
  324. .id = AV_CODEC_ID_DXA,
  325. .priv_data_size = sizeof(DxaDecContext),
  326. .init = decode_init,
  327. .close = decode_end,
  328. .decode = decode_frame,
  329. .capabilities = CODEC_CAP_DR1,
  330. };