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.

388 lines
13KB

  1. /*
  2. * Fraps FPS1 decoder
  3. * Copyright (c) 2005 Roine Gustafsson
  4. * Copyright (c) 2006 Konstantin Shishkov
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav 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. * Libav 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 Libav; 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 "bswapdsp.h"
  37. #include "internal.h"
  38. #define FPS_TAG MKTAG('F', 'P', 'S', 'x')
  39. /**
  40. * local variable storage
  41. */
  42. typedef struct FrapsContext {
  43. AVCodecContext *avctx;
  44. BswapDSPContext bdsp;
  45. AVFrame *frame;
  46. uint8_t *tmpbuf;
  47. int tmpbuf_size;
  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. avctx->pix_fmt = AV_PIX_FMT_NONE; /* set in decode_frame */
  58. s->avctx = avctx;
  59. s->tmpbuf = NULL;
  60. s->frame = av_frame_alloc();
  61. if (!s->frame)
  62. return AVERROR(ENOMEM);
  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, FF_HUFFMAN_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. init_get_bits(&gb, s->tmpbuf, size * 8);
  98. for (j = 0; j < h; j++) {
  99. for (i = 0; i < w*step; i += step) {
  100. dst[i] = get_vlc2(&gb, vlc.table, FF_HUFFMAN_BITS, 3);
  101. /* lines are stored as deltas between previous lines
  102. * and we need to add 0x80 to the first lines of chroma planes
  103. */
  104. if (j)
  105. dst[i] += dst[i - stride];
  106. else if (Uoff)
  107. dst[i] += 0x80;
  108. if (get_bits_left(&gb) < 0) {
  109. ff_free_vlc(&vlc);
  110. return AVERROR_INVALIDDATA;
  111. }
  112. }
  113. dst += stride;
  114. }
  115. ff_free_vlc(&vlc);
  116. return 0;
  117. }
  118. static int decode_frame(AVCodecContext *avctx,
  119. void *data, int *got_frame,
  120. AVPacket *avpkt)
  121. {
  122. FrapsContext * const s = avctx->priv_data;
  123. const uint8_t *buf = avpkt->data;
  124. int buf_size = avpkt->size;
  125. AVFrame *frame = data;
  126. AVFrame * const f = s->frame;
  127. uint32_t header;
  128. unsigned int version,header_size;
  129. unsigned int x, y;
  130. const uint32_t *buf32;
  131. uint32_t *luma1,*luma2,*cb,*cr;
  132. uint32_t offs[4];
  133. int i, j, ret, is_chroma, planes;
  134. enum AVPixelFormat pix_fmt;
  135. int prev_pic_bit, expected_size;
  136. if (buf_size < 4) {
  137. av_log(avctx, AV_LOG_ERROR, "Packet is too short\n");
  138. return AVERROR_INVALIDDATA;
  139. }
  140. header = AV_RL32(buf);
  141. version = header & 0xff;
  142. header_size = (header & (1<<30))? 8 : 4; /* bit 30 means pad to 8 bytes */
  143. prev_pic_bit = header & (1U << 31); /* bit 31 means same as previous pic */
  144. if (version > 5) {
  145. av_log(avctx, AV_LOG_ERROR,
  146. "This file is encoded with Fraps version %d. " \
  147. "This codec can only decode versions <= 5.\n", version);
  148. return AVERROR_PATCHWELCOME;
  149. }
  150. buf += 4;
  151. if (header_size == 8)
  152. buf += 4;
  153. pix_fmt = version & 1 ? AV_PIX_FMT_BGR24 : AV_PIX_FMT_YUVJ420P;
  154. if (avctx->pix_fmt != pix_fmt && f->data[0]) {
  155. av_frame_unref(f);
  156. }
  157. avctx->pix_fmt = pix_fmt;
  158. avctx->color_range = version & 1 ? AVCOL_RANGE_UNSPECIFIED
  159. : AVCOL_RANGE_JPEG;
  160. expected_size = header_size;
  161. switch (version) {
  162. case 0:
  163. default:
  164. /* Fraps v0 is a reordered YUV420 */
  165. if (!prev_pic_bit)
  166. expected_size += avctx->width * avctx->height * 3 / 2;
  167. if (buf_size != expected_size) {
  168. av_log(avctx, AV_LOG_ERROR,
  169. "Invalid frame length %d (should be %d)\n",
  170. buf_size, expected_size);
  171. return AVERROR_INVALIDDATA;
  172. }
  173. if (((avctx->width % 8) != 0) || ((avctx->height % 2) != 0)) {
  174. av_log(avctx, AV_LOG_ERROR, "Invalid frame size %dx%d\n",
  175. avctx->width, avctx->height);
  176. return AVERROR_INVALIDDATA;
  177. }
  178. if ((ret = ff_reget_buffer(avctx, f)) < 0) {
  179. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  180. return ret;
  181. }
  182. f->pict_type = prev_pic_bit ? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
  183. f->key_frame = f->pict_type == AV_PICTURE_TYPE_I;
  184. if (f->pict_type == AV_PICTURE_TYPE_I) {
  185. buf32 = (const uint32_t*)buf;
  186. for (y = 0; y < avctx->height / 2; y++) {
  187. luma1 = (uint32_t*)&f->data[0][ y * 2 * f->linesize[0]];
  188. luma2 = (uint32_t*)&f->data[0][(y * 2 + 1) * f->linesize[0]];
  189. cr = (uint32_t*)&f->data[1][ y * f->linesize[1]];
  190. cb = (uint32_t*)&f->data[2][ y * f->linesize[2]];
  191. for (x = 0; x < avctx->width; x += 8) {
  192. *(luma1++) = *(buf32++);
  193. *(luma1++) = *(buf32++);
  194. *(luma2++) = *(buf32++);
  195. *(luma2++) = *(buf32++);
  196. *(cr++) = *(buf32++);
  197. *(cb++) = *(buf32++);
  198. }
  199. }
  200. }
  201. break;
  202. case 1:
  203. /* Fraps v1 is an upside-down BGR24 */
  204. if (!prev_pic_bit)
  205. expected_size += avctx->width * avctx->height * 3;
  206. if (buf_size != expected_size) {
  207. av_log(avctx, AV_LOG_ERROR,
  208. "Invalid frame length %d (should be %d)\n",
  209. buf_size, expected_size);
  210. return AVERROR_INVALIDDATA;
  211. }
  212. if ((ret = ff_reget_buffer(avctx, f)) < 0) {
  213. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  214. return ret;
  215. }
  216. f->pict_type = prev_pic_bit ? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
  217. f->key_frame = f->pict_type == AV_PICTURE_TYPE_I;
  218. if (f->pict_type == AV_PICTURE_TYPE_I) {
  219. for (y = 0; y<avctx->height; y++)
  220. memcpy(&f->data[0][(avctx->height - y - 1) * f->linesize[0]],
  221. &buf[y * avctx->width * 3],
  222. 3 * avctx->width);
  223. }
  224. break;
  225. case 2:
  226. case 4:
  227. /**
  228. * Fraps v2 is Huffman-coded YUV420 planes
  229. * Fraps v4 is virtually the same
  230. */
  231. planes = 3;
  232. if ((ret = ff_reget_buffer(avctx, f)) < 0) {
  233. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  234. return ret;
  235. }
  236. /* skip frame */
  237. if (buf_size == 8) {
  238. f->pict_type = AV_PICTURE_TYPE_P;
  239. f->key_frame = 0;
  240. break;
  241. }
  242. f->pict_type = AV_PICTURE_TYPE_I;
  243. f->key_frame = 1;
  244. if ((AV_RL32(buf) != FPS_TAG) || (buf_size < (planes * 1024 + 24))) {
  245. av_log(avctx, AV_LOG_ERROR, "Fraps: error in data stream\n");
  246. return AVERROR_INVALIDDATA;
  247. }
  248. for (i = 0; i < planes; i++) {
  249. offs[i] = AV_RL32(buf + 4 + i * 4);
  250. if (offs[i] >= buf_size || (i && offs[i] <= offs[i - 1] + 1024)) {
  251. av_log(avctx, AV_LOG_ERROR, "Fraps: plane %i offset is out of bounds\n", i);
  252. return AVERROR_INVALIDDATA;
  253. }
  254. }
  255. offs[planes] = buf_size;
  256. for (i = 0; i < planes; i++) {
  257. is_chroma = !!i;
  258. av_fast_padded_malloc(&s->tmpbuf, &s->tmpbuf_size,
  259. offs[i + 1] - offs[i] - 1024);
  260. if (!s->tmpbuf)
  261. return AVERROR(ENOMEM);
  262. if ((ret = fraps2_decode_plane(s, f->data[i], f->linesize[i],
  263. avctx->width >> is_chroma,
  264. avctx->height >> is_chroma,
  265. buf + offs[i], offs[i + 1] - offs[i],
  266. is_chroma, 1)) < 0) {
  267. av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
  268. return ret;
  269. }
  270. }
  271. break;
  272. case 3:
  273. case 5:
  274. /* Virtually the same as version 4, but is for RGB24 */
  275. planes = 3;
  276. if ((ret = ff_reget_buffer(avctx, f)) < 0) {
  277. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  278. return ret;
  279. }
  280. /* skip frame */
  281. if (buf_size == 8) {
  282. f->pict_type = AV_PICTURE_TYPE_P;
  283. f->key_frame = 0;
  284. break;
  285. }
  286. f->pict_type = AV_PICTURE_TYPE_I;
  287. f->key_frame = 1;
  288. if ((AV_RL32(buf) != FPS_TAG)||(buf_size < (planes*1024 + 24))) {
  289. av_log(avctx, AV_LOG_ERROR, "Fraps: error in data stream\n");
  290. return AVERROR_INVALIDDATA;
  291. }
  292. for (i = 0; i < planes; i++) {
  293. offs[i] = AV_RL32(buf + 4 + i * 4);
  294. if (offs[i] >= buf_size || (i && offs[i] <= offs[i - 1] + 1024)) {
  295. av_log(avctx, AV_LOG_ERROR, "Fraps: plane %i offset is out of bounds\n", i);
  296. return AVERROR_INVALIDDATA;
  297. }
  298. }
  299. offs[planes] = buf_size;
  300. for (i = 0; i < planes; i++) {
  301. av_fast_padded_malloc(&s->tmpbuf, &s->tmpbuf_size,
  302. offs[i + 1] - offs[i] - 1024);
  303. if (!s->tmpbuf)
  304. return AVERROR(ENOMEM);
  305. if ((ret = fraps2_decode_plane(s, f->data[0] + i + (f->linesize[0] * (avctx->height - 1)),
  306. -f->linesize[0], avctx->width, avctx->height,
  307. buf + offs[i], offs[i + 1] - offs[i], 0, 3)) < 0) {
  308. av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
  309. return ret;
  310. }
  311. }
  312. // convert pseudo-YUV into real RGB
  313. for (j = 0; j < avctx->height; j++) {
  314. for (i = 0; i < avctx->width; i++) {
  315. f->data[0][0 + i*3 + j*f->linesize[0]] += f->data[0][1 + i*3 + j*f->linesize[0]];
  316. f->data[0][2 + i*3 + j*f->linesize[0]] += f->data[0][1 + i*3 + j*f->linesize[0]];
  317. }
  318. }
  319. break;
  320. }
  321. if ((ret = av_frame_ref(frame, f)) < 0)
  322. return ret;
  323. *got_frame = 1;
  324. return buf_size;
  325. }
  326. /**
  327. * closes decoder
  328. * @param avctx codec context
  329. * @return 0 on success or negative if fails
  330. */
  331. static av_cold int decode_end(AVCodecContext *avctx)
  332. {
  333. FrapsContext *s = (FrapsContext*)avctx->priv_data;
  334. av_frame_free(&s->frame);
  335. av_freep(&s->tmpbuf);
  336. return 0;
  337. }
  338. AVCodec ff_fraps_decoder = {
  339. .name = "fraps",
  340. .long_name = NULL_IF_CONFIG_SMALL("Fraps"),
  341. .type = AVMEDIA_TYPE_VIDEO,
  342. .id = AV_CODEC_ID_FRAPS,
  343. .priv_data_size = sizeof(FrapsContext),
  344. .init = decode_init,
  345. .close = decode_end,
  346. .decode = decode_frame,
  347. .capabilities = AV_CODEC_CAP_DR1,
  348. };