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.

357 lines
11KB

  1. /*
  2. * Fraps FPS1 decoder
  3. * Copyright (c) 2005 Roine Gustafsson
  4. * Copyright (c) 2006 Konstantin Shishkov
  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. /**
  23. * @file
  24. * Lossless Fraps 'FPS1' decoder
  25. * @author Roine Gustafsson (roine at users sf net)
  26. * @author Konstantin Shishkov
  27. *
  28. * Codec algorithm for version 0 is taken from Transcode <www.transcoding.org>
  29. *
  30. * Version 2 files support by Konstantin Shishkov
  31. */
  32. #include "config.h"
  33. #define CACHED_BITSTREAM_READER HAVE_FAST_64BIT
  34. #define UNCHECKED_BITSTREAM_READER 1
  35. #include "avcodec.h"
  36. #include "get_bits.h"
  37. #include "huffman.h"
  38. #include "bytestream.h"
  39. #include "bswapdsp.h"
  40. #include "internal.h"
  41. #include "thread.h"
  42. #define FPS_TAG MKTAG('F', 'P', 'S', 'x')
  43. #define VLC_BITS 11
  44. /**
  45. * local variable storage
  46. */
  47. typedef struct FrapsContext {
  48. AVCodecContext *avctx;
  49. BswapDSPContext bdsp;
  50. uint8_t *tmpbuf;
  51. int tmpbuf_size;
  52. } FrapsContext;
  53. /**
  54. * initializes decoder
  55. * @param avctx codec context
  56. * @return 0 on success or negative if fails
  57. */
  58. static av_cold int decode_init(AVCodecContext *avctx)
  59. {
  60. FrapsContext * const s = avctx->priv_data;
  61. s->avctx = avctx;
  62. s->tmpbuf = NULL;
  63. ff_bswapdsp_init(&s->bdsp);
  64. return 0;
  65. }
  66. /**
  67. * Comparator - our nodes should ascend by count
  68. * but with preserved symbol order
  69. */
  70. static int huff_cmp(const void *va, const void *vb)
  71. {
  72. const Node *a = va, *b = vb;
  73. return (a->count - b->count)*256 + a->sym - b->sym;
  74. }
  75. /**
  76. * decode Fraps v2 packed plane
  77. */
  78. static int fraps2_decode_plane(FrapsContext *s, uint8_t *dst, int stride, int w,
  79. int h, const uint8_t *src, int size, int Uoff,
  80. const int step)
  81. {
  82. int i, j, ret;
  83. GetBitContext gb;
  84. VLC vlc;
  85. Node nodes[512];
  86. for (i = 0; i < 256; i++)
  87. nodes[i].count = bytestream_get_le32(&src);
  88. size -= 1024;
  89. if ((ret = ff_huff_build_tree(s->avctx, &vlc, 256, VLC_BITS,
  90. nodes, huff_cmp,
  91. FF_HUFFMAN_FLAG_ZERO_COUNT)) < 0)
  92. return ret;
  93. /* we have built Huffman table and are ready to decode plane */
  94. /* convert bits so they may be used by standard bitreader */
  95. s->bdsp.bswap_buf((uint32_t *) s->tmpbuf,
  96. (const uint32_t *) src, size >> 2);
  97. if ((ret = init_get_bits8(&gb, s->tmpbuf, size)) < 0)
  98. return ret;
  99. for (j = 0; j < h; j++) {
  100. for (i = 0; i < w*step; i += step) {
  101. dst[i] = get_vlc2(&gb, vlc.table, VLC_BITS, 3);
  102. /* lines are stored as deltas between previous lines
  103. * and we need to add 0x80 to the first lines of chroma planes
  104. */
  105. if (j)
  106. dst[i] += dst[i - stride];
  107. else if (Uoff)
  108. dst[i] += 0x80;
  109. if (get_bits_left(&gb) < 0) {
  110. ff_free_vlc(&vlc);
  111. return AVERROR_INVALIDDATA;
  112. }
  113. }
  114. dst += stride;
  115. }
  116. ff_free_vlc(&vlc);
  117. return 0;
  118. }
  119. static int decode_frame(AVCodecContext *avctx,
  120. void *data, int *got_frame,
  121. AVPacket *avpkt)
  122. {
  123. FrapsContext * const s = avctx->priv_data;
  124. const uint8_t *buf = avpkt->data;
  125. int buf_size = avpkt->size;
  126. ThreadFrame frame = { .f = data };
  127. AVFrame * const f = data;
  128. uint32_t header;
  129. unsigned int version,header_size;
  130. unsigned int x, y;
  131. const uint32_t *buf32;
  132. uint32_t *luma1,*luma2,*cb,*cr;
  133. uint32_t offs[4];
  134. int i, j, ret, is_chroma;
  135. const int planes = 3;
  136. int is_pal;
  137. uint8_t *out;
  138. if (buf_size < 4) {
  139. av_log(avctx, AV_LOG_ERROR, "Packet is too short\n");
  140. return AVERROR_INVALIDDATA;
  141. }
  142. header = AV_RL32(buf);
  143. version = header & 0xff;
  144. is_pal = buf[1] == 2 && version == 1;
  145. header_size = (header & (1<<30))? 8 : 4; /* bit 30 means pad to 8 bytes */
  146. if (version > 5) {
  147. avpriv_report_missing_feature(avctx, "Fraps version %u", version);
  148. return AVERROR_PATCHWELCOME;
  149. }
  150. buf += header_size;
  151. if (is_pal) {
  152. unsigned needed_size = avctx->width * avctx->height + 1024;
  153. needed_size += header_size;
  154. if (buf_size != needed_size) {
  155. av_log(avctx, AV_LOG_ERROR,
  156. "Invalid frame length %d (should be %d)\n",
  157. buf_size, needed_size);
  158. return AVERROR_INVALIDDATA;
  159. }
  160. } else if (version < 2) {
  161. unsigned needed_size = avctx->width * avctx->height * 3;
  162. if (version == 0) needed_size /= 2;
  163. needed_size += header_size;
  164. /* bit 31 means same as previous pic */
  165. if (header & (1U<<31)) {
  166. *got_frame = 0;
  167. return buf_size;
  168. }
  169. if (buf_size != needed_size) {
  170. av_log(avctx, AV_LOG_ERROR,
  171. "Invalid frame length %d (should be %d)\n",
  172. buf_size, needed_size);
  173. return AVERROR_INVALIDDATA;
  174. }
  175. } else {
  176. /* skip frame */
  177. if (buf_size == 8) {
  178. *got_frame = 0;
  179. return buf_size;
  180. }
  181. if (AV_RL32(buf) != FPS_TAG || buf_size < planes*1024 + 24) {
  182. av_log(avctx, AV_LOG_ERROR, "error in data stream\n");
  183. return AVERROR_INVALIDDATA;
  184. }
  185. for (i = 0; i < planes; i++) {
  186. offs[i] = AV_RL32(buf + 4 + i * 4);
  187. if (offs[i] >= buf_size - header_size || (i && offs[i] <= offs[i - 1] + 1024)) {
  188. av_log(avctx, AV_LOG_ERROR, "plane %i offset is out of bounds\n", i);
  189. return AVERROR_INVALIDDATA;
  190. }
  191. }
  192. offs[planes] = buf_size - header_size;
  193. for (i = 0; i < planes; i++) {
  194. av_fast_padded_malloc(&s->tmpbuf, &s->tmpbuf_size, offs[i + 1] - offs[i] - 1024);
  195. if (!s->tmpbuf)
  196. return AVERROR(ENOMEM);
  197. }
  198. }
  199. f->pict_type = AV_PICTURE_TYPE_I;
  200. f->key_frame = 1;
  201. avctx->pix_fmt = version & 1 ? is_pal ? AV_PIX_FMT_PAL8 : AV_PIX_FMT_BGR24 : AV_PIX_FMT_YUVJ420P;
  202. avctx->color_range = version & 1 ? AVCOL_RANGE_UNSPECIFIED
  203. : AVCOL_RANGE_JPEG;
  204. avctx->colorspace = version & 1 ? AVCOL_SPC_UNSPECIFIED : AVCOL_SPC_BT709;
  205. if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
  206. return ret;
  207. switch (version) {
  208. case 0:
  209. default:
  210. /* Fraps v0 is a reordered YUV420 */
  211. if (((avctx->width % 8) != 0) || ((avctx->height % 2) != 0)) {
  212. av_log(avctx, AV_LOG_ERROR, "Invalid frame size %dx%d\n",
  213. avctx->width, avctx->height);
  214. return AVERROR_INVALIDDATA;
  215. }
  216. buf32 = (const uint32_t*)buf;
  217. for (y = 0; y < avctx->height / 2; y++) {
  218. luma1 = (uint32_t*)&f->data[0][ y * 2 * f->linesize[0] ];
  219. luma2 = (uint32_t*)&f->data[0][ (y * 2 + 1) * f->linesize[0] ];
  220. cr = (uint32_t*)&f->data[1][ y * f->linesize[1] ];
  221. cb = (uint32_t*)&f->data[2][ y * f->linesize[2] ];
  222. for (x = 0; x < avctx->width; x += 8) {
  223. *luma1++ = *buf32++;
  224. *luma1++ = *buf32++;
  225. *luma2++ = *buf32++;
  226. *luma2++ = *buf32++;
  227. *cr++ = *buf32++;
  228. *cb++ = *buf32++;
  229. }
  230. }
  231. break;
  232. case 1:
  233. if (is_pal) {
  234. uint32_t *pal = (uint32_t *)f->data[1];
  235. for (y = 0; y < 256; y++) {
  236. pal[y] = AV_RL32(buf) | 0xFF000000;
  237. buf += 4;
  238. }
  239. for (y = 0; y <avctx->height; y++)
  240. memcpy(&f->data[0][y * f->linesize[0]],
  241. &buf[y * avctx->width],
  242. avctx->width);
  243. } else {
  244. /* Fraps v1 is an upside-down BGR24 */
  245. for (y = 0; y<avctx->height; y++)
  246. memcpy(&f->data[0][(avctx->height - y - 1) * f->linesize[0]],
  247. &buf[y * avctx->width * 3],
  248. 3 * avctx->width);
  249. }
  250. break;
  251. case 2:
  252. case 4:
  253. /**
  254. * Fraps v2 is Huffman-coded YUV420 planes
  255. * Fraps v4 is virtually the same
  256. */
  257. for (i = 0; i < planes; i++) {
  258. is_chroma = !!i;
  259. if ((ret = fraps2_decode_plane(s, f->data[i], f->linesize[i],
  260. avctx->width >> is_chroma,
  261. avctx->height >> is_chroma,
  262. buf + offs[i], offs[i + 1] - offs[i],
  263. is_chroma, 1)) < 0) {
  264. av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
  265. return ret;
  266. }
  267. }
  268. break;
  269. case 3:
  270. case 5:
  271. /* Virtually the same as version 4, but is for RGB24 */
  272. for (i = 0; i < planes; i++) {
  273. if ((ret = fraps2_decode_plane(s, f->data[0] + i + (f->linesize[0] * (avctx->height - 1)),
  274. -f->linesize[0], avctx->width, avctx->height,
  275. buf + offs[i], offs[i + 1] - offs[i], 0, 3)) < 0) {
  276. av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
  277. return ret;
  278. }
  279. }
  280. out = f->data[0];
  281. // convert pseudo-YUV into real RGB
  282. for (j = 0; j < avctx->height; j++) {
  283. uint8_t *line_end = out + 3*avctx->width;
  284. while (out < line_end) {
  285. out[0] += out[1];
  286. out[2] += out[1];
  287. out += 3;
  288. }
  289. out += f->linesize[0] - 3*avctx->width;
  290. }
  291. break;
  292. }
  293. *got_frame = 1;
  294. return buf_size;
  295. }
  296. /**
  297. * closes decoder
  298. * @param avctx codec context
  299. * @return 0 on success or negative if fails
  300. */
  301. static av_cold int decode_end(AVCodecContext *avctx)
  302. {
  303. FrapsContext *s = (FrapsContext*)avctx->priv_data;
  304. av_freep(&s->tmpbuf);
  305. return 0;
  306. }
  307. AVCodec ff_fraps_decoder = {
  308. .name = "fraps",
  309. .long_name = NULL_IF_CONFIG_SMALL("Fraps"),
  310. .type = AVMEDIA_TYPE_VIDEO,
  311. .id = AV_CODEC_ID_FRAPS,
  312. .priv_data_size = sizeof(FrapsContext),
  313. .init = decode_init,
  314. .close = decode_end,
  315. .decode = decode_frame,
  316. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
  317. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
  318. };