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.

374 lines
12KB

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