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.

318 lines
9.7KB

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